<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

require __DIR__ . '/../autoload.php';

use h1cms\model\User;
use h1cms\model\UserProfile;
use Lg\Component\Paginator;
use Lg\Http\Input;
use Lg\Security\Password;
use Lg\View\View;

function indexAction() {
	$paginator = new Paginator(Input::get('page', 1), Input::get('limit', 20), Input::query());
	$query = db_table('users');
	$total_rows = $query->count();
	$query->limit($paginator->getLimit(), $paginator->getOffset());
	$users = $query->fetchAll();
	$paginator->setTotal($total_rows);
	screen()->addBreadcrumb(__('用户管理'), '', '', true);
	return View::make('index', [
		'users' => $users,
		'total_rows' => $total_rows,
		'paginator' => $paginator,
	]);
}

function addAction() {
	if (Input::isMethod('post')) {
//        if(__username_is_exists(Input::post('username'))){
		//            return redirect('/users/?action=add')->withFlash('error', '用户名已存在');
		//        }
		//        if(__email_is_exists(Input::post('email'))){
		//            return redirect('/users/?action=add')->withFlash('error', 'Email已存在');
		//        }
		$validator = AddUserValidator::make();
		if (!$validator->validate()) {
			return redirect('/users/?action=add')->withErrors($validator->errors());
		}
	}
	screen()->setTitle(__('添加用户'), __('用户管理'));
	screen()->addBreadcrumb(__('添加用户'), '', '', true);
	return View::make('add', []);
}

function editAction() {
	$id = intval(Input::get('id'));
	$user = User::findById($id);
	if (!$user) {
		return redirect('/users/')->withFlash('error', '用户不存在');
	}
	$user_profile = UserProfile::findById($id);
	if (Input::isMethod('post')) {
		if (($maybe_redirect = maybe_change_username($user, Input::post('username'), Input::post('email')))) {
			return $maybe_redirect;
		}
		$password = input_post('password');
		$repassword = input_post('repassword');
		if (!empty($password) && !empty($repassword) && strlen($password) >= 6 && $password == $repassword) {
			$user->password = Password::make($password);
		}
		$user->fill(Input::post('user'));
		$user_profile->fill(Input::post('user_profile'));
		$user_profile->save();
		$user->save();
		return redirect('/users/')->withFlash('success', '修改成功');
	}

	screen()->setTitle(__('编辑用户'), __('用户管理'));
	screen()->addBreadcrumb(__('编辑用户'), '', '', true);
	return View::make('form', [
		'user' => $user,
		'user_profile' => $user_profile,
	]);
}

function deleteAction() {
	$id = intval(Input::get('id'));
	if ($id == 1) {
		return redirect('/users/')->withFlash('error', '没有删除权限');
	}
	$user = User::findById($id);
	if (!$user) {
		return redirect('/users/')->withFlash('error', '用户不存在');
	}
}

function profileAction() {
	$id = \h1cms\system\Auth::user()->id;
	$user = User::findById($id);
	$user_profile = UserProfile::findById($id);
	if (Input::isMethod('post')) {
		$user->fill(Input::post('user'));
		$user_profile->fill(Input::post('user_profile'));
		$user_profile->save();
		$user->save();
		return redirect('/users/')->withFlash('success', '修改成功');
	}
	return View::make('profile', [
		'user' => $user,
		'user_profile' => $user_profile,
	]);
}

//上传头像
function avatarAction() {
	//上传到storage/tmp目录
	Input::file('avatar_file')->move(get_system_temp_dir(), md5(str_random()) . '.' . Input::file('avatar_file')->getClientOriginalExtension());
}
